/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.provider;

import org.rcgwt.client.ui.grid.IIndexList;
import org.rcgwt.client.ui.grid.event.BeforeCheckEvent;
import org.rcgwt.client.ui.grid.event.BeforeCheckHandler;
import org.rcgwt.client.ui.grid.event.CheckEvent;
import org.rcgwt.client.ui.grid.event.CheckHandler;
import org.rcgwt.client.ui.grid.event.IIndexesState;
import org.rcgwt.client.ui.grid.internal.HandlerManagerProvider;
import org.rcgwt.client.ui.grid.internal.IndexesState;
import org.rcgwt.client.ui.grid.internal.IntRangeList;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class BasicCheckProvider extends HandlerManagerProvider implements
        ICheckProvider {

    private final IntRangeList currentCheckedRows = new IntRangeList();

    public int[] getCheckedRowIndexes() {
        return currentCheckedRows.toArray();
    }

    public void setCheckedRowIndexes(int... rowIndexes) {

    }

    @Override
    public void appendCheckedRowIndexes(int... rowIndexes) {

    }

    @Override
    public void clearCheckedRowIndexes(int... rowIndexes) {

    }

    @Override
    public IIndexList getCheckedList() {
        return currentCheckedRows.clone();
    }

    @Override
    public HandlerRegistration addCheckHandler(
            CheckHandler<IIndexesState> handler) {
        return ensureHandlers().addHandler(CheckEvent.getType(), handler);
    }

    @Override
    public HandlerRegistration addBeforeCheckHandler(
            BeforeCheckHandler<IIndexesState> handler) {
        return ensureHandlers().addHandler(BeforeCheckEvent.getType(), handler);

    }

    /**
     * Internal call
     * 
     * @param newChecked
     * @param unchecked
     * @param allUnselected
     * @return
     */
    protected boolean processSelection(IntRangeList newChecked,
            IntRangeList unchecked) {

        assert newChecked != null : "NewChecked list is NULL";
        assert unchecked != null : "UnChecked list is NULL";

        IndexesState checkState = new IndexesState(currentCheckedRows,
                newChecked, unchecked);

        if (getHandlerCount(BeforeCheckEvent.getType()) > 0) {
            // perform before

            BeforeCheckEvent<IIndexesState> event = BeforeCheckEvent.fire(this,
                    checkState);

            if (event.isCanceled()) {
                return false;
            }
        }

        if (unchecked.isEmpty() == false) {
            currentCheckedRows.remove(unchecked);
        }

        if (newChecked.isEmpty() == false) {
            currentCheckedRows.addInterval(newChecked);
        }

        Log.debug("Change CHECK to=" + currentCheckedRows);

        // if (getHandlerCount(SelectionEvent.getType()) > 0) {
        // perform after

        CheckEvent.fire(this, checkState);
        // }

        return true;

    }

}
